package com.softmarket.action.manager;

import java.io.IOException;

import org.seasar.framework.exception.IORuntimeException;
import org.seasar.framework.util.ResourceUtil;
import org.seasar.struts.annotation.Execute;

import com.softmarket.action.BaseAction;
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;

public class LoginAction extends BaseAction {

	public String userName;

	public String password;

	@Execute(validator = false)
	public String index() {
		return "../login.jsp";
	}

	@Execute(validator = false)
	public String login() throws IOException {
		try {
			String lUserName = new String(Base64.decode(ResourceUtil
					.getProperties("softmarket.properties")
					.getProperty("system.username").getBytes()));
			String lPassword = new String(Base64.decode(ResourceUtil
					.getProperties("softmarket.properties")
					.getProperty("system.password").getBytes()));

			if (userName.equals(lUserName) && password.equals(lPassword)) {
				request.getSession().setAttribute("loginFlg", "true");
				response.getWriter().write("success");
			}
		} catch (IORuntimeException e) {
			e.printStackTrace();
		} catch (Base64DecodingException e) {
			e.printStackTrace();
		}

		return null;
	}

	@Execute(validator = false)
	public String logout() throws IOException {
		request.getSession().removeAttribute("loginFlg");
		response.sendRedirect(request.getContextPath() + "/manager/login");
		return null;
	}

	public static void main(String [] args) {
		try {
			System.out.println( new String(Base64.decode(ResourceUtil
					.getProperties("softmarket.properties")
					.getProperty("system.username").getBytes())));

			System.out.println( new String(Base64.decode(ResourceUtil
					.getProperties("softmarket.properties")
					.getProperty("system.password").getBytes())));

			System.out.println(new String(Base64.encode("".getBytes())));
		} catch (IORuntimeException e) {
			e.printStackTrace();
		} catch (Base64DecodingException e) {
			e.printStackTrace();
		}

	}
}
